package hu.cskassai.sad.gui.panels;

import hu.cskassai.sad.business.GraphManager;
import hu.cskassai.sad.gui.frames.MainFrame;
import hu.cskassai.sad.gui.mouse.MyModalGraphMouse;
import hu.cskassai.sad.model.graph.Edge;
import hu.cskassai.sad.model.graph.Vertex2D;

import java.awt.Dimension;
import java.awt.geom.Ellipse2D;

import org.apache.commons.collections15.functors.ConstantTransformer;

import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.algorithms.layout.StaticLayout;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.decorators.EdgeShape;

public class GraphVisualizerPanel extends VisualizationViewer<Vertex2D, Edge> {
	
	private final GraphManager<Vertex2D, Edge> graphManager;
	
	public GraphVisualizerPanel(final Layout<Vertex2D, Edge> layout, final Dimension preferredSize,
		final GraphManager<Vertex2D, Edge> graphManager, MainFrame mainFrame)
	{
		super(layout, preferredSize);
		this.graphManager = graphManager;
		final MyModalGraphMouse modalGraphMouse = new MyModalGraphMouse(renderContext, graphManager, mainFrame);
		setGraphMouse(modalGraphMouse);
		renderContext.setEdgeShapeTransformer(new EdgeShape.Line<Vertex2D, Edge>());
		renderContext.setVertexShapeTransformer(new ConstantTransformer(
			new Ellipse2D.Float(-5, -5, 10, 10)));
		renderContext.setEdgeDrawPaintTransformer(graphManager.getEdgeColorTransformer());
		renderContext.setVertexFillPaintTransformer(graphManager.getVertexColorTransformer());
		
	}
	
	public void changeGraph() {
		final StaticLayout<Vertex2D, Edge> layout = new StaticLayout<Vertex2D, Edge>(graphManager.getGraph());
		layout.setInitializer(graphManager.getVertexToPointTransformer());
		setGraphLayout(layout);
		this.repaint();
	}
	
}
